package com.tdkj.action.backstage.information;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanInfoComment;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.information.IBeanSpotGoodService;
public class SpotGoodAction  extends BaseAction{
	private long id;
	private BeanBaseInfo spotgood;
	private String varietyid;
	private String variety;
	private String fullId;
	private Timestamp createtime;
	private Map<String,String>prop=new HashMap<String,String>();
	private IBeanSpotGoodService spotgoodService;
	private List<BeanBaseInfo> spotgoods=new ArrayList<BeanBaseInfo>();
	private String[] weightUnit={"元/吨","元/公斤","元/克"};
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanInfoComment.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getVarietyid() {
		return varietyid;
	}
	public void setVarietyid(String varietyid) {
		this.varietyid = varietyid;
	}
	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
	public String getFullId() {
		return fullId;
	}
	public void setFullId(String fullId) {
		this.fullId = fullId;
	}
	public Timestamp getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Timestamp createtime) {
		this.createtime = createtime;
	}
	public String[] getWeightUnit() {
		return weightUnit;
	}
	public void setWeightUnit(String[] weightUnit) {
		this.weightUnit = weightUnit;
	}
	public BeanBaseInfo getSpotgood() {
		return spotgood;
	}
	public void setSpotgood(BeanBaseInfo spotgood) {
		this.spotgood = spotgood;
	}
	public IBeanSpotGoodService getSpotgoodService() {
		return spotgoodService;
	}
	@Resource
	public void setSpotgoodService(IBeanSpotGoodService spotgoodService) {
		this.spotgoodService = spotgoodService;
	}
	public List<BeanBaseInfo> getSpotgoods() {
		return spotgoods;
	}
	public void setSpotgoods(List<BeanBaseInfo> spotgoods) {
		this.spotgoods = spotgoods;
	}
	public Map<String, String> getProp() {
		return prop;
	}
	public void setProp(Map<String, String> prop) {
		this.prop = prop;
	}
	public String findAll(){
		return "findAll";
	}
	public String findRightPage(){
		if(fullId!=null){
			String hql = "select bean from " + BeanBaseInfo.class.getName() +" as bean, " + BeanType.class.getName() + " as type "
			+ " where bean.varietyid=type.id and (type.fullId='"+fullId+"' or type.fullId like '"+fullId+"-%')" 
			+ " and bean.moduletype='14' and bean.columntype='2' order by moduletype desc";
			Query query = this.createQuery(hql);
			this.queryByPage(query, "spotgoodAction!findRightPage.action", 10);
		}else{
			Criteria criteria = this.createCriteria(BeanBaseInfo.class);
			criteria.add(Restrictions.eq("moduletype", "14"));
			criteria.add(Restrictions.eq("columntype", "2"));
			criteria.addOrder(Order.desc("moduletype"));// 
			this.queryByPage(criteria, "spotgoodAction!findRightPage.action", 10);
		}
		return "findRightPage";
	}
	public String findRightAddPage(){
		//spotgoods=spotgoodService.findSpotGoods();
		return "findRightAddPage";
	}
	
	public String findRightEditPage(){
		spotgood=spotgoodService.findById(this.getId(),BeanBaseInfo.class);
		return "findRightEditPage";
	}
	public String getTree(){
		return "tree";
	}
	public String addSpotgood(){
		BeanBaseInfo bean=new BeanBaseInfo();
		bean.setClicktime(1);
		bean.setModuletype("14");
		bean.setModulename("行情");
		bean.setColumntype("2");
		bean.setColumnname("现货行情");
		bean.setCreatetime(this.getCreatetime());
		bean.setStatus("未审核");
		if(this.getProp()!=null){
			bean.setProp(this.getProp());
		}
		//BeanUser u= (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		BeanUser u=spotgoodService.findById(1l, BeanUser.class);
		bean.setUser(u);
		spotgoodService.save(bean);
		return findAll();
	}
	
	public String editSpotgood(){
		BeanBaseInfo bean=spotgoodService.findById(this.getId(), BeanBaseInfo.class);
		bean.setVariety(this.getVariety());
		bean.setVarietyid(this.getVarietyid());
		bean.setCreatetime(this.getCreatetime());
		if(this.getProp()!=null){
			bean.setProp(this.getProp());
		}
		spotgoodService.merge(bean);
		return findAll();
	}
	
	public String deleteSpotgood(){
		spotgoodService.deleteSpotGood(this.getId());
		return findRightPage();
	}
	
}
